﻿$(document).ready(
    function () {

        function NewsViewModel() {
            var self = this;
            //field obs
            self.news = ko.observableArray();
            getData(self.news);
            //fiellds
            self.current = ko.observable(new ObservableArrayItem({ Id: "", Title: "", Text: "", Created: "", Url: "" }));
            self.old = { Id: "", Text: "", Title: "", Created: "", Url: "" };
            //no observ fields
            self.updatePanelVisible = false;
            self.createPanelVisible = false;
            //function
            self.removeItem = function (place) {
                $.post("Delete", { id: place.Id }, function (returnedData) {
                    if (returnedData.success == true)
                        self.news.remove(place);
                    else showError();
                }).error(function () {
                    showError();
                });
            };
            //
            self.updateItem = function (updateitem) {
                self.old.Id = updateitem.Id();
                self.old.Text = updateitem.Text();
                self.old.Title = updateitem.Title();
                self.old.Created = updateitem.Created();
                self.old.Url = updateitem.Url();
                self.current(updateitem);
            };
            //
            self.updateCurrentItem = function () {
                $.post("Update",
                    {
                        Id: self.current().Id(),
                        Text: self.current().Text(),
                        Title: self.current().Title(),
                        Created: self.current().Created()
                    }, function (returnedData) {
                        if (!returnedData.success == true) {
                            self.cancelChanges();
                            showError("Operation update is failed");
                        }
                    }).error(function () {
                        self.cancelChanges();
                        showError("Operation update is failed");
                    });
            };
            //
            self.cancelChanges = function () {
                self.current().Id(self.old.Id);
                self.current().Text(self.old.Text);
                self.current().Title(self.old.Title);
                self.current().Created(self.old.Created);
                self.current().Url(self.old.Url);
            };
            //
            self.createItem = function () {
                var title = $('#titleCreateor').val();
                var text = $('#textCreateor').val();
                $.post("Create", { Title: title, Text: text }, function (returnedData) {
                    if (!returnedData.success == true) {
                        showError("Operation create is failed");
                    } else {
                        var newitem = returnedData.data;
                        newitem.Url = '/News/MoreInformation/' + newitem.Id;
                        var obsvitem = new ObservableArrayItem(newitem);
                        self.news.push(obsvitem);
                    }
                }).error(function () {
                    showError("Operation create is failed");
                });
            };
            //hide show panels
            self.hsCreatePanel = function () {
                var panel = $('#createPanel');
                var button = $('#hsCreatePanel');
                self.createPanelVisible = changeState(panel, button, self.createPanelVisible);
            };
            //
            self.hsUpdatePanel = function () {
                var panel = $('#updatePanel');
                var button = $('#hsUpdatePanel');
                self.updatePanelVisible = changeState(panel, button, self.updatePanelVisible);
            };
            //utility for show or hide panel
            function changeState(panel, button, stateFlag) {
                if (stateFlag) {
                    panel.hide(150);
                    button.removeClass('hide_showbutton_ico_visible');
                    button.addClass('hide_showbutton_ico_hidden');
                    return false;
                } else {
                    panel.show(150);
                    button.removeClass('hide_showbutton_ico_hidden');
                    button.addClass('hide_showbutton_ico_visible');
                    return true;
                }
            }


            return self;
        }

        function getData(array) {

            $.getJSON("/News/GetJsonNews", function (data) {
                for (var i = 0; i < data.length; i++) {
                    data[i].Url = '/News/MoreInformation/' + data[i].Id;
                    var a = new ObservableArrayItem(data[i]);
                    array.push(a);
                };
            });
        }

        function showError(message) {
            Ext.Msg.show({
                title: 'Error',
                msg: message,
                buttons: Ext.Msg.OK,
                icon: Ext.MessageBox.ERROR
            });
        }
        ko.applyBindings(new NewsViewModel());

        function ObservableArrayItem(object) {
            this.Id = ko.observable(object.Id);
            this.Title = ko.observable(object.Title);
            this.Text = ko.observable(object.Text);
            this.Created = ko.observable(object.Created);
            this.Url = ko.observable(object.Url);
        }
    }
)